package com.hzjh.h5mall.ui.wallet

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.data.Configs
import com.hzjh.base.ext.data.UserConfigs
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.frame.adapter.MutableAdapter
import com.hzjh.base.frame.adapter.SimpleGapItemDecor
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.utils.function.dp2px
import com.hzjh.base.utils.function.visibleOrGone
import com.hzjh.h5mall.BuildConfig
import com.hzjh.h5mall.adapter.wallet.WalletClassTypeVH
import com.hzjh.h5mall.databinding.ActivityWalletBinding
import com.hzjh.h5mall.databinding.ItemWalletClassBinding
import com.hzjh.h5mall.model.wallet.WalletVM
import com.hzjh.service.metamask.MetaMaskUtils

@Route(path = RouteUrl.WALLET_ACTIVITY)
class WalletActivity : JHBaseActivity<ActivityWalletBinding,WalletVM>() {
    private val classBalanceAdapter by lazy {
        MutableAdapter(viewModel.classBalanceList.value!!).apply {
            addVhDelegate {
                WalletClassTypeVH(ItemWalletClassBinding.inflate(layoutInflater, it, false))
            }
        }
    }
    override fun initData() {
        viewModel.initMetaMask()
        viewModel.reqMyWallet()
    }

    override fun initView() {
        viewBinding.rvClassBalance.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        viewBinding.rvClassBalance.addItemDecoration(
            SimpleGapItemDecor(horGap = 7.dp2px())
        )
        viewBinding.rvClassBalance.adapter=classBalanceAdapter

        viewBinding.tvWithdrawal.setOnClickListener {
            //提现
            navigationTo(RouteUrl.WITHDRAWAL_ACTIVITY){
                withString(RouteUrl.PARAMS_BALANCE,viewModel.walletBean.value?.remainingMoney?:"")
                withString(RouteUrl.PARAMS_METAMASK_NO,viewModel.walletBean.value?.metamastNo?:"")
            }
        }
        viewBinding.tvRecharge.setOnClickListener {
            //充值
            navigationTo(RouteUrl.RECHARGE_ACTIVITY){
                withString(RouteUrl.PARAMS_BALANCE,viewModel.walletBean.value?.remainingMoney?:"")
            }
        }
        viewBinding.llAddMetamask.setOnClickListener {
            //添加metaMask账号
            MetaMaskUtils.getInstance()?.reqConnect{
                viewModel.reqAddMetaMask(it)
            }
        }
        viewBinding.llMetamaskLayout.ivDeleteTag.setOnClickListener {
            //删除metaMask账号
            viewModel.reqDeleteMetaMask(viewModel.walletBean.value?.metamastNo?:"")
        }
        viewBinding.tvRecords.setOnClickListener {
            //提现记录
            navigationTo(RouteUrl.WEB_ACTIVITY){
                withString(RouteUrl.PARAMS_WEB_URL, BuildConfig.h5Url+ Configs.WALLET_ORDER_LIST_URL)
                withBoolean(RouteUrl.PARAMS_WEB_BAR_VISIBILITY,false)
                withBoolean(RouteUrl.PARAMS_WEB_TITLE_BAR_VISIBILITY,false)
            }
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.walletBean.observe(this){
            viewBinding.tvWalletTitle.text=UserConfigs.getUserData().email
            viewBinding.tvUsdtBlance.show(it.remainingMoney?:"0.00")
            viewModel.getClassBalanceList(it.profitMoney,it.adMoney,it.boxMoney)
            viewBinding.llAddMetamask.visibleOrGone(it.bindMetamask==0)
            viewBinding.flMetamaskLayout.visibleOrGone(it.bindMetamask==1){
                viewBinding.llMetamaskLayout.tvMetamaskNo.text=it.metamastNo?:""
            }
        }
        viewModel.classBalanceList.observe(this){
            classBalanceAdapter.notifyDataSetChanged()
        }
    }

    override fun initViewBinding(): ActivityWalletBinding {
        return ActivityWalletBinding.inflate(layoutInflater)
    }
}